package com.ml.ws.service;


import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.ml.ws.bo.AgentCustInfo;

@Service
public class CommonService {

	
	public List<AgentCustInfo> queryAgentCustInfo(String memberId, String custId, String prodType, String custAbbrName, String custFullName, String custStatus, int pageNum, int pageSize, boolean all) throws Exception {
//		if(memberId == null){
//			return new ArrayList();
//		}
//		
//		String sql = "select c.status, c.holder_id, c.holder_exch_id, c.cn_name, c.cn_name_abbr, c.en_name, c.en_name_abbr,"
//				+ " c.client_type, c.type1, c.type2, c.reg_addr, c.license_no, c.org_license_no, c.financial_license_no,"
//				+ " c.other_cert, c.other_cert_no, c.contact1, c.contact_no1, c.email1, c.fax1, c.contact2, c.contact_no2,"
//				+ " c.email2, c.fax2, c.addr, c.zip, c.variety_id from (select t1.member_id, t2.* ,rownum rn  from trade.tt_client_member t1, trade.tt_client t2 where t1.client_id=t2.client_id ) c"
//				+ " where c.member_id='"+memberId+"'";
//		
//		if(custId != null && !"".equals(custId)){
//			sql += " and c.client_id='"+custId+"'";
//		}
//		if(prodType != null && !"".equals(prodType)){
//			sql += " and ','||c.variety_id||',' like '%,"+prodType+",%'";
//		}
//		if(custAbbrName != null && !"".equals(custAbbrName)){
//			sql += " and c.cn_name_abbr='"+custAbbrName+"'";
//		}
//		if(custFullName != null && !"".equals(custFullName)){
//			sql += " and c.cn_name='"+custFullName+"'";
//		}
//		if(custStatus != null && !"".equals(custStatus)){
//			sql += " and c.status='"+custStatus+"'";
//		}
//		if(!all){
//			sql += " and c.rn >= "+((pageNum-1)*pageSize) + " and c.rn <= " +(pageNum*pageSize);
//		}
//		List<AgentCustInfo> infos = new ArrayList<AgentCustInfo>();
		List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
		for(int i=0;i<10;i++){
//			AgentCustInfo info = new AgentCustInfo();
//			info.setClient_type("client_type"+i);
//			info.setCn_name("cn_name"+i);
//			info.setCn_name_abbr("cn_name_abbr"+i);
//			info.setContact1("contact1"+i);
//			info.setContact2("contact2"+i);
//			info.setContact_no1("contact_no1"+i);
//			info.setContact_no2("contact_no2"+i);
//			info.setEmail1("email1"+i);
//			info.setEmail2("email2"+i);
//			info.setEn_name("en_name"+i);
//			info.setEn_name_abbr("en_name_abbr"+i);
//			info.setFax1("fax1"+i);
//			info.setFax2("fax2"+i);
//			info.setFinancial_license_no("financial_license_no"+i);
//			info.setHolder_exch_id("holder_exch_id"+i);
//			info.setHolder_id("holder_id"+i);
//			info.setLicense_no("license_no"+i);
//			info.setMember_id("member_id"+i);
//			info.setOrg_license_no("org_license_no"+i);
//			info.setOther_cert("other_cert"+i);
//			info.setOther_cert_no("other_cert_no"+i);
//			info.setReg_addr("reg_addr"+i);
//			info.setStatus("status"+i);
//			info.setType1("type1"+i);
//			info.setType2("type2"+i);
//			info.setVariety_id("variety_id"+i);
//			info.setZip("zip"+i);
//			infos.add(info);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("client_type","client_type"+i);
			map.put("cn_name","cn_name"+i);
			map.put("cn_name_abbr","cn_name_abbr"+i);
			map.put("contact1","contact1"+i);
			map.put("contact2","contact2"+i);
			map.put("contact_no1","contact_no1"+i);
			map.put("contact_no2","contact_no2"+i);
			map.put("email1","email1"+i);
			map.put("email2","email2"+i);
			map.put("en_name","en_name"+i);
			map.put("en_name_abbr","en_name_abbr"+i);
			map.put("fax1","fax1"+i);
			map.put("fax2","fax2"+i);
			map.put("financial_license_no","financial_license_no"+i);
			map.put("holder_exch_id","holder_exch_id"+i);
			map.put("holder_id","holder_id"+i);
			map.put("license_no","license_no"+i);
			map.put("member_id","member_id"+i);
			map.put("org_license_no","org_license_no"+i);
			map.put("other_cert","other_cert"+i);
			map.put("other_cert_no","other_cert_no"+i);
			map.put("reg_addr","reg_addr"+i);
			map.put("status","status"+i);
			map.put("type1","type1"+i);
			map.put("type2","type2"+i);
			map.put("variety_id","variety_id"+i);
			map.put("zip","zip"+i);
			maps.add(map);
		}
		
		List<AgentCustInfo> infos = new ArrayList<AgentCustInfo>();
		for(Map<String, Object> map : maps){
			AgentCustInfo info = new AgentCustInfo();
			transResult(map, info);
			infos.add(info);
		}
		
//		return transResult(jdbcTemplate.queryForList(sql), AgentCustInfo.class.getName());
		return infos;
	}
	
	
	private void transResult(Map<String, Object> map, Object obj){
		
		for(String key : map.keySet()){
			key.toLowerCase();
		}
		
		for (Field field : obj.getClass().getDeclaredFields()) {
			final String name = field.getName();
			if (name.equalsIgnoreCase("serialVersionUID")) {
				continue;
			}
			field.setAccessible(true);
			try {
				field.set(obj, map.get(name));
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
}
